package array_slice

import "fmt"

func LengthCap() {
	// 示例1。
	s1 := make([]int, 5)
	fmt.Printf("The length of s1: %d\n", len(s1))  //5
	fmt.Printf("The capacity of s1: %d\n", cap(s1)) //5
	fmt.Printf("The value of s1: %d\n", s1)  //[0 0 0 0 0 ]
	s2 := make([]int, 5, 8)
	fmt.Printf("The length of s2: %d\n", len(s2)) //5
	fmt.Printf("The capacity of s2: %d\n", cap(s2))	//8
	fmt.Printf("The value of s2: %d\n", s2) //[0 0 0 0 0 ]

	s3 := []int{1, 2, 3, 4, 5, 6, 7, 8}
	s4 := s3[3:6]                                   //索引范围是从3到5 [3,6)
	fmt.Printf("The length of s4: %d\n", len(s4))   //3
	fmt.Printf("The capacity of s4: %d\n", cap(s4)) //5
	fmt.Printf("The value of s4: %d\n", s4)         //[4,5,6]
}

func ExtendDemo() {
	s6 := make([]int, 0)
	fmt.Printf("The capacity of s6: %d\n", cap(s6)) //0
	for i := 1; i <= 5; i++ {
		s6 = append(s6, i)
		fmt.Printf("s6(%d): len: %d, cap: %d\n", i, len(s6), cap(s6)) //1 2 4 4 8
	}
	fmt.Println()

	s7 := make([]int, 1024)
	fmt.Printf("The capacity of s7: %d\n", cap(s7))  //1024
	s7e1 := append(s7, make([]int, 200)...)
	fmt.Printf("s7e1: len: %d, cap: %d\n", len(s7e1), cap(s7e1)) //1224  1280
	s7e2 := append(s7, make([]int, 400)...)
	fmt.Printf("s7e2: len: %d, cap: %d\n", len(s7e2), cap(s7e2)) //1424  1696 (1024*1.25*1.25=1600)
	s7e3 := append(s7, make([]int, 600)...)
	fmt.Printf("s7e3: len: %d, cap: %d\n", len(s7e3), cap(s7e3))//1624  2048
	fmt.Println()

	s8 := make([]int, 10)
	fmt.Printf("The capacity of s8: %d\n", cap(s8))//10
	s8a := append(s8, make([]int, 11)...)
	fmt.Printf("s8a: len: %d, cap: %d\n", len(s8a), cap(s8a))//21 22
	s8b := append(s8a, make([]int, 23)...)
	fmt.Printf("s8b: len: %d, cap: %d\n", len(s8b), cap(s8b))//44 44
	s8c := append(s8b, make([]int, 45)...)
	fmt.Printf("s8c: len: %d, cap: %d\n", len(s8c), cap(s8c))//89,96
}
func ExtendChange()  {
	a1 := [7]int{1, 2, 3, 4, 5, 6, 7}
	fmt.Printf("a1: %v (len: %d, cap: %d)\n",
		a1, len(a1), cap(a1))
	s9 := a1[1:4]
	//s9[0] = 1
	fmt.Printf("s9: %v (len: %d, cap: %d)\n",
		s9, len(s9), cap(s9))
	for i := 1; i <= 5; i++ {
		s9 = append(s9, i)
		fmt.Printf("s9(%d): %v (len: %d, cap: %d)\n",
			i, s9, len(s9), cap(s9))
	}
	fmt.Printf("a1: %v (len: %d, cap: %d)\n",
		a1, len(a1), cap(a1))
	fmt.Println()
}

func Copy()  {
	s1 :=[]int{1,2,3,4}
    s2 := s1[0:4]
    s2[1]=22
    fmt.Println(s1) //[1 22 3 4]

    s3 :=[]int{1,2,3,4}

    dst:=make([]int, 4)

    resInt:=copy(dst ,s3)
    fmt.Printf("resInt=%d\n",resInt)
    dst[1]=22222

    fmt.Println(s3) //s3 未改变


}